# def curve_pre():  # 闭包函数
#     a = 25  # 环境变量
#
#     def curve(x):
#         return a * x * x
#
#     return curve
#
#
# f = curve_pre()
# print(f(2))
#
# print(f.__closure__)

#
# def f1():
#     b = 2
#
#     def f2():
#         b = 3   # 外包的变量只能被引用，修改的话就再是闭包
#         return b
#     return f2
#
#
# nf = f1()
# print(nf)
# print(nf.__closure__)

original = 0


def factory(pos):
    def go(step):
        nonlocal pos  # 强制定义参数为非本地局部变量
        new_pos = pos + step
        pos = new_pos
        return new_pos
    return go


f = factory(original)
print(f(2))
print(f(3))
print(f(5))

origin = 0


def f1(step):
    global origin
    origin = origin + step
    return origin


print(f1(2))
print(f1(3))
print(f1(5))

# def f1(a):
#     def f2(step):
#         new_pops = a + step
#         a = new_pops
#         return a
#
#     return f2
#
#
# f = f1(original)
# print(f(2))
# print(f(3))
# print(f(5))
